package com.thinkTank.oa.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thinkTank.oa.entity.Conference;
import com.thinkTank.oa.entity.Dictiontry;
import com.thinkTank.oa.entity.Person;
import com.thinkTank.oa.entity.User;
import com.thinkTank.oa.service.ConferenceService;
import com.thinkTank.oa.service.PersonService;
import com.thinkTank.oa.service.UserService;
import com.thinkTank.oa.util.Json;



@Controller
public class TestController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(TestController.class);
	@Resource
	private PersonService personService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private ConferenceService conferenceService;

//	@ RequestMapping(value= "index")
//	public String index(HttpServletRequest request){
//		 /***
//		  * 获取项目类型前台显示
//		  */
//		 List<Dictiontry> dicList = getProjectSorce();
//		 request.setAttribute("dicList", dicList);
//		 /***
//		  * 前台显示省份
//		  */
//		 List<Dictiontry>  areaPriv= getAreaProvince();
//		 request.setAttribute("areaPriv", areaPriv);
//		return "starter";
//	}
//	

	@ RequestMapping(value= "index1")
	public String index(HttpServletRequest request){
		//Person person = personService.getById(1);
		//System.out.println(person.getAge());
		 /***
		  * 获取项目类型前台显示
		  */
		 List<Dictiontry> dicList = getProjectSorce();
		 request.setAttribute("dicList", dicList);
		 /***
		  * 前台显示省份
		  */
		 List<Dictiontry>  areaPriv= getAreaProvince();
		 request.setAttribute("areaPriv", areaPriv);
		return "starter";
	}

	@ RequestMapping(value= "updataDistPickerData")
	public String updataDistpickerData(HttpServletRequest request){
		//Person person = personService.getById(1);
		//System.out.println(person.getAge());

		/***
		 * 获取所有省份
		 */
		List<Dictiontry>  areaPriv= getAreaProvince();
		StringBuilder sb=new StringBuilder();
		sb.append(" var DISTRICTS = {\r\n" +
				"    1: {\r\n");
		for (int i=0;i<areaPriv.size();i++ ) {
			Dictiontry prov=areaPriv.get(i);
			sb.append("      "+prov.getNames()+": '"+prov.getWorth()+"'");
			if(i<areaPriv.size()-1){
				sb.append(",\r\n");
			}
		}
		sb.append("\r\n},\r\n");

		for (Dictiontry prov :areaPriv) {
			sb.append("      "+prov.getNames()+": {\r\n");
			List<Dictiontry> cityList = getCityByProvince(prov.getNames());
			for (int i=0;i<cityList.size();i++) {
				Dictiontry city=cityList.get(i);
				sb.append("      "+city.getNames()+": '"+city.getWorth()+"'");
				if(i<cityList.size()-1){
					sb.append(",\r\n");
				}
			}
			sb.append("\r\n},\r\n");
			}


		for (Dictiontry prov :areaPriv) {
			List<Dictiontry> cityList = getCityByProvince(prov.getNames());
			for (int j=0;j<cityList.size();j++ ) {
				Dictiontry city=cityList.get(j);
				List<Dictiontry> areas =getAreaByCity(city.getNames());
				sb.append("      "+city.getNames()+": {\r\n");
				for (int i=0;i<areas.size();i++) {
					Dictiontry area=areas.get(i);
					sb.append("      "+area.getNames()+": '"+area.getWorth()+"'");
					if(i<areas.size()-1){
						sb.append(",\r\n");
					}
				}
				sb.append("\r\n},\r\n");
			}
		}
		String s=sb.toString();
		s=s.substring(0,s.length()-3)+"\r\n  };";
		//List<Dictiontry> cityList = getCityByProvince(id);
		System.out.print(s);
		return "starter";
	}



//  根据省份id获取城市数据后直接使用@ResponseBody装成json数据
  @RequestMapping(value = "/getCityByProvinceId/{id}")
  @ResponseBody
  public Json getCityByProvinceId(@PathVariable("id") String id){
      List<Dictiontry> cityList = getCityByProvince(id);
      if (cityList!=null){
          return new Json(true,"success",cityList);
      } else {
          return new Json(false,"fail",null);
      }
  }
  //根据城市id获取区域数据后直接使用@ResponseBody装成json数据
  @RequestMapping(value = "/getAreaByCityId/{id}")
  @ResponseBody
  public Json getAreaByCityId(@PathVariable("id") String id){
	  List<Dictiontry> areaList = getAreaByCity(id);
      if (areaList!=null){
          return new Json(true,"success",areaList);
      } else {
          return new Json(false,"fail",null);
      }
  }

//	
//	@ RequestMapping(value= "login")
//	public String login(HttpServletRequest request){
//		System.out.println(request.getHeader("Referer"));
//		//Person person = personService.getById(1);
//		System.out.println("没有权限-------------");
//		return "login";
//	}
//	
//	@ RequestMapping(value= "checkLogin")
//	public String checkLogin(HttpServletRequest request){
//		//Person person = personService.getById(1);
//		System.out.println("888888888888888888888888888888888888888");
//		return "starter";
//	}
//

	
	@ RequestMapping(value= "login1")
	public String login(HttpServletRequest request){
		System.out.println(request.getHeader("Referer"));
		//Person person = personService.getById(1);
		System.out.println("没有权限-------------");
		return "login";
	}

	@ RequestMapping(value= "hello")
	public String hello(HttpServletRequest request){
		 /***
		  * 获取项目类型前台显示
		  */
		 List<Dictiontry> dicList = getProjectSorce();
		 request.setAttribute("dicList", dicList);
		 /***
		  * 前台显示省份
		  */
		 List<Dictiontry>  areaPriv= getAreaProvince();
		 request.setAttribute("areaPriv", areaPriv);
		return "starter";

	}

//	@RequestMapping(value= "addUser")
//	public String addUser(User user){
//		
//		//System.out.println(user.getPassWord());
//		user.setMd5("E10ADC3949BA59ABBE56E057F20F883E");
//		//user.setPersonId(2);
//		System.out.println("55555555555555555555555555555");
//		userService.add(user);
//		return "userList";
//	}

	@RequestMapping(value= "addUser")
	public String addUser(User user){
		
//		System.out.println(user.getPassWord());
		user.setMd5("E10ADC3949BA59ABBE56E057F20F883E");
//		user.setPersonId(2);
		System.out.println("55555555555555555555555555555");
		userService.add(user);
		return "userList";
	}

	@RequestMapping("/showAllUser")
	public String showAllUser(HttpServletRequest request){
		 PageHelper.startPage(1, 1);
		 User user = new User();
		 List<User> list = userService.queryAll(user);
		 System.out.println("user数量：" + list.size());
		 PageInfo<User> pageInfo = new PageInfo<User>(list);
		 request.setAttribute("userList", pageInfo);
			return "showUserList";
	}
	/**
	 * 冯晶晶测试方法
	 * @param request
	 * @return
	 */
	@RequestMapping("/tabdemo")
	public String tabdemo(HttpServletRequest request){
		
			return "tabdemo";
	}
	
	/**
	 * 冯晶晶测试方法 json
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/fjjtestpersonjson",produces= "application/json;charset=UTF-8")
	public List fjjtestpersonjson(HttpServletRequest request){
		Person person=new Person();
		List<Person> list = personService.findMPersonList(person);
			return list;
	}
	
	/**
	 * 冯晶晶测试方法 json
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/fjjtestconfjson",produces= "application/json;charset=UTF-8")
	public List fjjtestconfjson(HttpServletRequest request){
		Conference conference=new Conference();
		List<Conference> list = conferenceService.queryAllConference(conference);
			return list;
	}
}
